package com.xxq.semester.controller.question;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxq.semester.entity.question.Questions;
import com.xxq.semester.entity.utils.ResponseEntity;
import com.xxq.semester.service.question.IQuestionsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author saki
 * @since 2025-07-13
 */
@RestController("questionQuestionsController")
@RequestMapping("/api/questions")
@Tag(name="试题管理", description = "试题增删改查接口")
public class QuestionsController {

    @Autowired
    private IQuestionsService questionsService;

    @GetMapping("/all")
    @Operation(summary = "获取所有试题", description = "获取所有试题的详细信息")
    public ResponseEntity<PageInfo<Questions>> getAllQuestions(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        List<Questions> questions = questionsService.selectAllQuestions();
        System.out.println(questionsService.selectAllQuestions());
        PageInfo<Questions> questionList = new PageInfo<>(questions);
        return ResponseEntity.success("查询成功", questionList);
    }

    @GetMapping("/{questionId}")
    @Operation(summary = "获取指定ID的试题", description = "根据试题ID获取试题的详细信息")
    public ResponseEntity<Questions> getQuestionByQuestionId(
            @Parameter(description = "试题ID") @PathVariable Integer questionId) {

        Questions questions = questionsService.selectQuestionsByQuestionId(questionId);
        if (questions == null) {
            return ResponseEntity.fail("试题不存在");
        } else {
            return ResponseEntity.success("查询成功", questions);
        }
    }

    @GetMapping("/creator/{creatorId}")
    @Operation(summary = "获取指定创建者的试题", description = "根据创建者ID获取该创建者创建的所有试题")
    public ResponseEntity<PageInfo<Questions>> getQuestionsByCreatorId(
            @Parameter(description = "创建者ID") @PathVariable Integer creatorId) {

        List<Questions> questions = questionsService.selectQuestionsByCreatorId(creatorId);
        PageInfo<Questions> questionList = new PageInfo<>(questions);
        if (questions.isEmpty()) {
            return ResponseEntity.fail("没有找到该创建者的试题");
        } else {
            return ResponseEntity.success("查询成功", questionList);
        }
    }

    @PostMapping("/add")
    @Operation(summary = "添加新试题", description = "添加一个新的试题到数据库")
    public ResponseEntity<String> addQuestion(@RequestBody Questions question) {
        if (question == null || question.getContent() == null || question.getCreatorId() == null) {
            return ResponseEntity.fail("试题内容或创建者ID不能为空");
        }
        question.setCreatedAt(LocalDateTime.now());
        boolean isAdded = questionsService.addQuestion(question);
        if (isAdded) {
            return ResponseEntity.success("试题添加成功");
        } else {
            return ResponseEntity.fail("试题添加失败");
        }
    }

    @DeleteMapping("/delete/{questionId}")
    @Operation(summary = "删除试题", description = "根据试题ID删除指定的试题")
    public ResponseEntity<String> deleteQuestionById(
            @Parameter(description = "试题ID") @PathVariable Integer questionId) {

        if (questionId == null) {
            return ResponseEntity.fail("试题ID不能为空");
        }
        boolean isDeleted = questionsService.deleteQuestionById(questionId);
        if (isDeleted) {
            return ResponseEntity.success("试题删除成功");
        } else {
            return ResponseEntity.fail("试题删除失败");
        }
    }

    @PutMapping("/update")
    @Operation(summary = "更新试题信息", description = "根据试题ID更新指定的试题信息")
    public ResponseEntity<String> updateQuestion(
            @RequestBody Questions updatedQuestion) {

        if (updatedQuestion.getQuestionId()== null ) {
            return ResponseEntity.fail("试题ID或更新信息不能为空");
        }
        else{
            boolean isUpdated = questionsService.updateQuestion(updatedQuestion);
            if (isUpdated) {
                return ResponseEntity.success("试题更新成功");
            } else {
                return ResponseEntity.fail("试题更新失败");
            }
        }
    }
}
